package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import static com.example.demo.common.R.success;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 用户表(SysUser)表控制层
 *
 * @author makejava
 * @since 2025-08-26 17:24:12
 */
@Tag(name = "用户管理", description = "系统用户相关的CRUD操作")
@RestController
@RequestMapping("sysUser")
public class SysUserController{
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param sysUser 查询实体
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有用户数据", description = "根据条件分页查询系统用户列表")
    @Parameters({
        @Parameter(name = "current", in = ParameterIn.QUERY, description = "当前页码", required = false, schema = @Schema(type = "integer", defaultValue = "1")),
        @Parameter(name = "size", in = ParameterIn.QUERY, description = "每页显示数量", required = false, schema = @Schema(type = "integer", defaultValue = "10")),
        @Parameter(name = "username", in = ParameterIn.QUERY, description = "用户名", required = false, schema = @Schema(type = "string")),
        @Parameter(name = "email", in = ParameterIn.QUERY, description = "邮箱", required = false, schema = @Schema(type = "string"))
    })
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping
    public R selectAll(Page<SysUser> page, SysUser sysUser) {
        return success(this.sysUserService.page(page, new QueryWrapper<>(sysUser)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条用户数据", description = "根据用户ID获取用户详情")
    @Parameter(name = "id", in = ParameterIn.PATH, description = "用户ID", required = true, schema = @Schema(type = "integer"))
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.sysUserService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增用户数据", description = "添加新的系统用户")
    @ApiResponse(responseCode = "200", description = "新增成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PostMapping
    public R insert(@RequestBody SysUser sysUser) {
        return success(this.sysUserService.save(sysUser));
    }

    /**
     * 修改数据
     *
     * @param sysUser 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改用户数据", description = "更新系统用户信息")
    @ApiResponse(responseCode = "200", description = "修改成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PutMapping
    public R update(@RequestBody SysUser sysUser) {
        return success(this.sysUserService.updateById(sysUser));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @Operation(summary = "删除用户数据", description = "批量删除系统用户")
    @Parameter(name = "idList", in = ParameterIn.QUERY, description = "用户ID列表", required = true, schema = @Schema(type = "array", implementation = Long.class))
    @ApiResponse(responseCode = "200", description = "删除成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.sysUserService.removeByIds(idList));
    }
}

